<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var layer; 

    function test_initialize(t) {
        t.plan( 2 );
        
        layer = new OpenLayers.Layer.Markers('Test Layer');
        t.ok( layer instanceof OpenLayers.Layer.Markers, "new OpenLayers.Layer.Markers returns object" );
        t.eq( layer.name, "Test Layer", "layer.name is correct" );
    }
    function test_addlayer (t) {
        t.plan( 3 );
        
        layer = new OpenLayers.Layer.Markers('Test Layer');
        t.ok( layer instanceof OpenLayers.Layer.Markers, "new OpenLayers.Layer.Markers returns object" );
        t.eq( layer.name, "Test Layer", "layer.name is correct" );
        layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), 
                                              new OpenLayers.Icon())
                       );
        t.eq( layer.markers.length, 1, "addLayer adds marker to layer." );
    }
    function test_addMarker_removeMarker (t) {
        t.plan( 6 );

        var map = new OpenLayers.Map('map');
        var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
            "http://octo.metacarta.com/cgi-bin/mapserv?",
            {map: "/mapdata/vmap_wms.map", layers: "basic"});
        map.addLayer(baseLayer);
        map.zoomToMaxExtent();
        layer = new OpenLayers.Layer.Markers('Test Layer');
        map.addLayer(layer);
        var marker = new OpenLayers.Marker(new OpenLayers.LonLat(5,40));
        layer.addMarker(marker);
        t.ok(  marker.icon.imageDiv.parentNode == layer.div, "addMarker adds marker image node into layer node." ); 
        layer.removeMarker(marker);
        t.ok(  marker.icon.imageDiv.parentNode != layer.div, "removeMarker removes marker image node from layer node." ); 
        layer.removeMarker(marker);
        t.ok(true, "Removing marker twice does not fail.");
        layer.addMarker(marker);
        t.ok(  marker.icon.imageDiv.parentNode == layer.div, "addMarker adds marker image node into layer node." ); 

        layer.markers = null;
        layer.removeMarker(marker);
        t.ok(true, "removing marker when no markers present does not script error");

        var l = new OpenLayers.Layer.Markers();
        var marker = new OpenLayers.Marker(new OpenLayers.LonLat(5,40));
        l.addMarker(marker);
        l.removeMarker(marker);
        t.ok(true, "Removing marker when layer not added to map does not fail.");

    }
    
    function test_markerMovement(t) {
        
        t.plan(6);
        
        var map = new OpenLayers.Map("map", {zoomMethod: null});
        var layer = new OpenLayers.Layer.Markers("Base", {isBaseLayer: true});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0, 0), 1);
        
        var size = new OpenLayers.Size(10, 10);
        var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
        var icon = new OpenLayers.Icon("foo", size, offset);
        var marker = new OpenLayers.Marker(new OpenLayers.LonLat(10, -10), icon)
        layer.addMarker(marker);
        
        t.eq(marker.icon.px.x, 554, "marker icon is placed at 554 px on x-axis");
        t.eq(marker.icon.px.y, 314, "marker icon is placed at 314 px on y-axis");
        
        map.zoomTo(2);
        
        t.eq(marker.icon.px.x, 568, "marker icon moved to 568 px on x-axis");
        t.eq(marker.icon.px.y, 328, "marker icon moved to 328 px on y-axis");
        
        map.zoomTo(1);

        t.eq(marker.icon.px.x, 554, "marker icon moved back to 554 px on x-axis");
        t.eq(marker.icon.px.y, 314, "marker icon moved back to 314 px on y-axis");
        
    }
    
    function test_destroy (t) {
        t.plan( 1 );    
        layer = new OpenLayers.Layer.Markers('Test Layer');
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);
        layer.destroy();
        t.eq( layer.map, null, "layer.map is null after destroy" );
    }

    function test_getDataExtent(t) {
        t.plan( 4 );

        var layer = {};
        var ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.eq(ret, null, "does not crash, returns null on layer with null 'this.markers'");

        layer.markers = [];
        ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.eq(ret, null, "returns null on layer with empty 'this.markers'");
        
        layer.markers.push({
            'lonlat': new OpenLayers.LonLat(4,5)
        });
        var expectedBounds = new OpenLayers.Bounds(4,5,4,5);
        ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.ok(ret.equals(expectedBounds), "returns expected bounds with only one marker");

        layer.markers.push({
            'lonlat': new OpenLayers.LonLat(1,2)
        });
        var expectedBounds = new OpenLayers.Bounds(1,2,4,5);
        ret = OpenLayers.Layer.Markers.prototype.getDataExtent.apply(layer, []);
        t.ok(ret.equals(expectedBounds), "returns expected bounds with multiple markers");

    }

    function test_setOpacity(t) {
        t.plan(1);

        layer = new OpenLayers.Layer.Markers('Test Layer');

        var opacity = 0.1234;
        
        for (var i = 0; i < 12; i++) {
            layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), new OpenLayers.Icon()));
        }

        layer.setOpacity(opacity);

        for (var i = 0; i < 4; i++) {
            layer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0), new OpenLayers.Icon()));
        }
        
        var itWorks = false;
        for (var i = 0; i < layer.markers.length; i++) {
            itWorks = parseFloat(layer.markers[i].icon.imageDiv.style.opacity) == opacity;
            if (!itWorks) {
                break;
            }
        }
        t.ok(itWorks, "setOpacity change markers opacity");
    }

  </script>
</head>
<body>
  <div id="map" style="width: 1080px; height: 600px;"/>
</body>
</html>
